local skel = fk.CreateSkill {
  name = "efengqi__qizhen",
}

Fk:loadTranslationTable{
  ["efengqi__qizhen"] = "旂阵",
  [":efengqi__qizhen"] = "你可以将一张基本牌当另一张基本牌使用或打出，然后可以将手牌调整至与当前回合角色相同并视为对其使用第三种基本牌。",

  ["#efengqi__qizhen"] = "旂阵：可以将一张基本牌当另一张基本牌使用或打出",
  ["#efengqi__qizhen-draw"] = "旂阵：你可摸 %arg 张牌，并视为对 %dest 使用第三种基本牌",
  ["#efengqi__qizhen-discard"] = "旂阵：你可弃置 %arg 张手牌，并视为对 %dest 使用第三种基本牌",

  ["$efengqi__qizhen1"] = "为将者，自当尽忠尽职。",
  ["$efengqi__qizhen2"] = "一人一枪一匹马，疆场尽驰骋。",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = ".|.|.|.|.|basic",
  prompt = "#efengqi__qizhen",
  interaction = function (self, player)
    local all_choices = Fk:getAllCardNames("b")
    local choices = player:getViewAsCardNames(self.name, all_choices)
    if #choices > 0 then
      return UI.CardNameBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeBasic and self.interaction.data
    and Fk:cloneCard(self.interaction.data).trueName ~= Fk:getCardById(to_select).trueName
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return nil end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_play = function(self, player)
    return true
  end,
  enabled_at_response = function (self, player, response)
    return not player:isKongcheng()
  end,
  before_use = function (self, player, use)
    use.extra_data = use.extra_data or {}
    use.extra_data.efengqi__qizhen_name = {use.card.trueName, Fk:getCardById(use.card.subcards[1]).trueName}
  end,
  after_use = function (self, player, use)
    local room = player.room
    local current = room:getCurrent()
    if not (use.extra_data and use.extra_data.efengqi__qizhen_name) or player.dead
     or not current or current.dead or current:getHandcardNum() == player:getHandcardNum() then return end
    local exclude = use.extra_data.efengqi__qizhen_name
    local all_choices = table.filter(Fk:getAllCardNames("b"), function (name)
      return not table.contains(exclude, Fk:cloneCard(name).trueName)
    end)
    local choices = table.filter(all_choices, function (name)
      local card = Fk:cloneCard(name)
      card.skillName = skel.name
      return player:canUseTo(card, current, {bypass_distances = true, bypass_times = true})
    end)
    if #choices == 0 then return end
    local num = player:getHandcardNum() - current:getHandcardNum()
    if num > 0 and #table.filter(player:getCardIds("h"), function (id)
      return not player:prohibitDiscard(id)
    end) < num then return end
    local prompt = (num > 0 and "#efengqi__qizhen-discard" or "#efengqi__qizhen-draw") .. "::" .. current.id .. ":" .. math.abs(num)
    local chosen = room:askToChoices(player, {
      min_num = 1, max_num = 1, choices = choices, all_choices = all_choices, cancelable = true, skill_name = skel.name,
      prompt = prompt,
    })[1]
    if chosen == nil then return end
    player:broadcastSkillInvoke(skel.name)
    if num > 0 then
      room:askToDiscard(player, { max_num = num, min_num = num, include_equip = false, skill_name = skel.name, cancelable = false})
    elseif num < 0 then
      player:drawCards(-num, skel.name)
    end
    room:delay(600)
    room:useVirtualCard(chosen, nil, player, current, skel.name, true)
  end,
})



return skel
